//
//  NetworkTool.swift
//  SwiftAFNPackage
//
//  Created by Zhu Jinxin on 16/7/21.
//  Copyright © 2016年 Zhu Jinxin. All rights reserved.
//

import UIKit
import AFNetworking

//swift 枚举
enum NetworkType : String {
    case GET = "GET"
    case POST = "POST"
}

class NetworkTool: AFHTTPSessionManager {
    
    static let sharedTools : NetworkTool = {
        
        let tools = NetworkTool()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools;
        
    }()
    
    
    func request(type :NetworkType, urlStr:String , parameters :AnyObject?,success:(responseObject :AnyObject?)->(),failClosure :(error:NSError)->()) {
        
        
        if type == .GET {
            
            self.GET(urlStr, parameters: parameters, progress: nil, success: { (_, res) in
                
                success(responseObject: res)
                
                }, failure: { (_, NSError) in
                    
                    failClosure(error: NSError)
                    
            })
            
        }else{
            
            self.POST(urlStr, parameters: parameters, progress: nil, success: { (_, res) in
                
                success(responseObject: res)
                
                }, failure: { (_, NSError) in
                    
                    failClosure(error: NSError)
                    
            })
            
        }
        
    }
    
}
